﻿Public Class frmViewDescriptionCase

    Public _Case As Long
    Public _isPalintiff As Boolean = False
    Public _isDefendant As Boolean = False
    Public _isThirdParty As Boolean = False
    Public _DescriptionCasePlaintiffID As Long
    Public _DescriptionCaseDefendantID As Long
    Public _DescriptionCaseThirdPartyID As Long

    Public _ContractID As Long
    Public _DescriptionContractID As Long

    Dim dt As New DataTable
    Dim j As Integer = 0
    Dim tmp As Integer

    Private Sub frmViewDescription_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        j = 0
        If _isPalintiff Then
            getDescription_Plaintiff()
        ElseIf _isDefendant Then
            getDescription_Defendant()
        ElseIf _isThirdParty Then
            getDescription_ThirdParty()
        End If
    End Sub

    Protected Sub getDescription_Plaintiff()
        Try
            getDescription_Plaintiff_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _DescriptionCasePlaintiffID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _DescriptionCasePlaintiffID = dt.Rows(i)("DescriptionCasePlaintiffID") Then
                            _DescriptionCasePlaintiffID = dt.Rows(i)("DescriptionCasePlaintiffID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            rtbDescription.Rtf = dt.Rows(i)("DescriptionName")
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានសេចក្តីអធិប្យាយបង្ហាញទេ", "បង្ហាញសេចក្តីអធិប្បាយ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getDescription_Defendant()
        Try
            getDescription_Defendant_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _DescriptionCaseDefendantID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _DescriptionCaseDefendantID = dt.Rows(i)("DescriptionCaseDefendantID") Then
                            _DescriptionCaseDefendantID = dt.Rows(i)("DescriptionCaseDefendantID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            rtbDescription.Rtf = dt.Rows(i)("DescriptionName")
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានសេចក្តីអធិប្យាយបង្ហាញទេ", "បង្ហាញសេចក្តីអធិប្បាយ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getDescription_ThirdParty()
        Try
            getDescription_thirdParty_byCaseID()
            Dim i As Integer
            If dt.Rows.Count > 0 Then
                If _DescriptionCaseThirdPartyID <> 0 Then
                    For i = 0 To dt.Rows.Count - 1
                        If _DescriptionCaseThirdPartyID = dt.Rows(i)("DescriptionCaseThirdPartyID") Then
                            _DescriptionCaseThirdPartyID = dt.Rows(i)("DescriptionCaseThirdPartyID")
                            lblUserID.Text = dt.Rows(i)("UserID")
                            lblUploadDate.Text = Format(dt.Rows(i)("UploadDate"), "dd-MM-yyyy hh:mm tt")
                            lblNo.Text = i + 1 & " of " & dt.Rows.Count
                            rtbDescription.Rtf = dt.Rows(i)("DescriptionName")
                            Exit For
                        End If
                        j += 1
                    Next
                Else
                    MessageBox.Show("គ្មានសេចក្តីអធិប្យាយបង្ហាញទេ", "បង្ហាញសេចក្តីអធិប្បាយ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub getDescription_CasePlaintiff_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _DescriptionCasePlaintiffID = dt.Rows(j)("DescriptionCasePlaintiffID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblUploadDate.Text = Format(dt.Rows(j)("UploadDate"), "dd-MM-yyyy hh:mm tt")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count
            rtbDescription.Rtf = dt.Rows(j)("DescriptionName")
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getDescription_CaseDefendant_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _DescriptionCaseDefendantID = dt.Rows(j)("DescriptionCaseDefendantID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblUploadDate.Text = Format(dt.Rows(j)("UploadDate"), "dd-MM-yyyy hh:mm tt")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count
            rtbDescription.Rtf = dt.Rows(j)("DescriptionName")
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getDescription_CaseThirdParty_Move()
        If j >= 0 And j < dt.Rows.Count Then
            _DescriptionCaseThirdPartyID = dt.Rows(j)("DescriptionCaseThirdPartyID")
            lblUserID.Text = dt.Rows(j)("UserID")
            lblUploadDate.Text = Format(dt.Rows(j)("UploadDate"), "dd-MM-yyyy hh:mm tt")
            lblNo.Text = j + 1 & " of " & dt.Rows.Count
            rtbDescription.Rtf = dt.Rows(j)("DescriptionName")
        Else
            j = tmp
        End If
    End Sub

    Protected Sub getDescription_Plaintiff_byCaseID()
        Dim oDescription As New cDescription
        dt = oDescription.select_Description_CasePlaintiff_byCaseID(_Case)
    End Sub

    Protected Sub getDescription_Defendant_byCaseID()
        Dim oDescription As New cDescription
        dt = oDescription.select_Description_CaseDefendant_byCaseID(_Case)
    End Sub

    Protected Sub getDescription_thirdParty_byCaseID()
        Dim oDescription As New cDescription
        dt = oDescription.select_Description_CaseThirdParty_byCaseID(_Case)
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        tmp = j
        j = 0
        If _isPalintiff Then
            getDescription_CasePlaintiff_Move()
        ElseIf _isDefendant Then
            getDescription_CaseDefendant_Move()
        ElseIf _isThirdParty Then
            getDescription_CaseThirdParty_Move()
        End If
    End Sub

    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        tmp = j
        j -= 1
        If _isPalintiff Then
            getDescription_CasePlaintiff_Move()
        ElseIf _isDefendant Then
            getDescription_CaseDefendant_Move()
        ElseIf _isThirdParty Then
            getDescription_CaseThirdParty_Move()
        End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        tmp = j
        j += 1
        If _isPalintiff Then
            getDescription_CasePlaintiff_Move()
        ElseIf _isDefendant Then
            getDescription_CaseDefendant_Move()
        ElseIf _isThirdParty Then
            getDescription_CaseThirdParty_Move()
        End If
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        tmp = j
        j = dt.Rows.Count - 1
        If _isPalintiff Then
            getDescription_CasePlaintiff_Move()
        ElseIf _isDefendant Then
            getDescription_CaseDefendant_Move()
        ElseIf _isThirdParty Then
            getDescription_CaseThirdParty_Move()
        End If
    End Sub

    Private Sub rtbDescription_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles rtbDescription.KeyDown
        e.SuppressKeyPress = True
    End Sub

    Private Sub tsHighlight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsHighlight.Click
        rtbDescription.SelectionBackColor = Color.Yellow
    End Sub

    Private Sub tsNotHighlight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsNotHighlight.Click
        rtbDescription.SelectionBackColor = Color.White
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If _isPalintiff Then
            update_Description_CasePlaintiff()
        ElseIf _isDefendant Then
            update_Description_CaseDefendant()
        ElseIf _isThirdParty Then
            update_Description_CaseThirdParty()
        End If
    End Sub

    Protected Sub update_Description_CasePlaintiff()
        If _DescriptionCasePlaintiffID <> 0 Then
            Dim oDescription As New cDescription
            If oDescription.update_Description_CasePlaintiff(_DescriptionCasePlaintiffID, rtbDescription.Rtf) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getDescription_CasePalintiff()
                getDescription_Plaintiff_byCaseID()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Protected Sub update_Description_CaseDefendant()
        If _DescriptionCaseDefendantID <> 0 Then
            Dim oDescription As New cDescription
            If oDescription.update_Description_CaseDefendant(_DescriptionCaseDefendantID, rtbDescription.Rtf) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getDescription_CaseDefendant()
                getDescription_Defendant_byCaseID()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Protected Sub update_Description_CaseThirdParty()
        If _DescriptionCaseThirdPartyID <> 0 Then
            Dim oDescription As New cDescription
            If oDescription.update_Description_CaseThirdParty(_DescriptionCaseThirdPartyID, rtbDescription.Rtf) Then
                MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                frmCaseEntry.getDescription_CaseThirdParty()
                getDescription_thirdParty_byCaseID()
            Else
                MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub
End Class